package com.qf.shixi.controller;

import com.qf.shixi.entity.SysUser;
import com.qf.shixi.result.ResponseCode;
import com.qf.shixi.result.ResponseData;
import com.qf.shixi.service.SysUserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * (SysUser)表控制层
 *
 * @author
 * @since 2021-10-17 23:01:48
 */
@RestController
@RequestMapping("sysUser")
public class SysUserController {

    /**
     * 服务对象
     */
    @Resource
    private SysUserService sysUserService;

    @ApiOperation(value = "微信登陆")
    @GetMapping("/api/login/{code}")
    public ResponseData wxLogin(@PathVariable String code) throws IOException {
        String token = sysUserService.wxLogin(code);
        if(token != null){
            return new ResponseData(token);
        }else{
            return new ResponseData(ResponseCode.ERROR);
        }
    }

    @ApiOperation(value = "修改用户信息,如果不存在,则直接添加")
    @PutMapping("/api/update")
    public ResponseData updateWxUserInfo(@RequestBody SysUser sysUser){
        Boolean update = sysUserService.update(sysUser);
        if(update){
            return new ResponseData();
        }else{
            return new ResponseData(ResponseCode.ERROR);
        }

    }

    @ApiOperation(value = "后台登陆")
    @PostMapping("/login")
    public ResponseData sysLogin(@RequestBody SysUser sysUser){
        SysUser sysUser1 =  sysUserService.sysLogin(sysUser);
        return new ResponseData(sysUser1);

    }




}
